package cn.edu.cug.cs.gtl.gsm.item;

import cn.edu.cug.cs.gtl.io.Storable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.Arrays;

public class ByteArray implements Storable {

    private static final long serialVersionUID = -6374548331330639788L;

    byte [] bytes;

    public ByteArray(byte[] bytes) {
        this.bytes = bytes;
    }
    public ByteArray() {
        this.bytes = new byte[1];
    }

    public byte[] getBytes(){
        return bytes;
    }
    @Override
    public Object clone() {
        ByteArray b = new ByteArray(Arrays.copyOf(bytes,bytes.length));
        return b;
    }

    @Override
    public boolean load(DataInput dataInput) throws IOException {
        int c = dataInput.readInt();
        bytes = new byte[c];
        dataInput.readFully(bytes);
        return true;
    }

    @Override
    public boolean store(DataOutput dataOutput) throws IOException {
        dataOutput.writeInt(bytes.length);
        dataOutput.write(bytes);
        return true;
    }
}
